package com.lmz.order.web;

import com.lmz.order.service.OrderService;
import com.lmz.order.vo.OrderConfirmVo;
import com.lmz.order.vo.OrderSubmitVo;
import com.lmz.order.vo.SubmitOrderResponseVo;
import net.sf.jsqlparser.statement.select.MinusOp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", orderConfirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model) {
        //创建订单，验证令牌，价格，锁定库存等操作
        SubmitOrderResponseVo orderResponseVo = orderService.submitOrder(vo);
        if (orderResponseVo.getCode() == 0) {
            //成功,跳转支付页
            model.addAttribute("submitOrderResp", orderResponseVo);
            return "pay";
        } else {
            //失败回到确认页面重新确认订单信息
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}